package com.decades.application.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.decades.application.dto.BlogDto;
import com.decades.application.main.CalendarMainActivity;
import com.decades.application.main.R;
import com.squareup.picasso.Picasso;

@SuppressLint("NewApi") public class GridViewMenuAdapter extends BaseAdapter {

	private Activity _activity;
	private List<BlogDto> menus;
	private int imageWidth;
	private ViewHolder viewHolder;

	public GridViewMenuAdapter(Activity activity, List<BlogDto> menus,int imageWidth) {
		this._activity = activity;
		this.menus = menus;
		this.imageWidth = imageWidth;
	}

	@Override
	public int getCount() {
		return this.menus.size();
	}

	@Override
	public Object getItem(int position) {
		return this.menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		BlogDto menu = menus.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder(); 
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.each_box_layout, null);
			viewHolder.relative = (RelativeLayout) convertView.findViewById(R.id.clickLayout);
			viewHolder.titleImage = (TextView) convertView.findViewById(R.id.textView1);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.imageView1);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag(); 
		}
		
		Picasso.with(_activity.getApplicationContext()).load(menu.getContent()).placeholder(R.drawable.defaultmenu).into(viewHolder.showCase);			

		convertView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, (imageWidth+45)));		
		viewHolder.titleImage.setText(menu.getAuthor_name());
		viewHolder.relative.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(_activity,CalendarMainActivity.class);
				i.putExtra("ActionBarTitle", _activity.getResources().getString(R.string.calendar_view));
				_activity.startActivity(i);
			}
		});
		return convertView;
	}

	public class ViewHolder {
		private ImageView showCase;
		private TextView titleImage;
		private RelativeLayout relative;
	}
	
}
